﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCOA.Helper;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.IO;
namespace MVCOA.Areas.Manage.Controllers
{
    public class Cate_FirstController : Controller
    {
        //
        // GET: /Manage/Cate_First/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Getdata(FormCollection fc)
        {
            IBLL.ICate_FisrtBLL first = OperateContext.Current.BLLSession.ICate_FisrtBLL;
            List<MODEL.Cate_Fisrt> list = first.GetListBy(q => true).Select(s => s.ToPOCO()).ToList();
            string sortname = fc["sortname"];
            string sortorder = fc["sortorder"];
            if (sortname != null && sortorder != null)
            {
                if (sortorder == "asc")//xiao->da
                {
                    if (sortname == "id") { list = first.GetListBy(q => true).OrderBy(s => s.id).Select(s => s.ToPOCO()).ToList(); }
                    if (sortname == "name") { list = first.GetListBy(q => true).OrderBy(s => s.name).Select(s => s.ToPOCO()).ToList(); }
                    if (sortname == "isNav") { list = first.GetListBy(q => true).OrderBy(s => s.isNav).Select(s => s.ToPOCO()).ToList(); }
                 //   if (sortname == "parentName") { list = first.GetListBy(q => true).OrderBy(s => s.secondId).Select(s => s.ToPOCO2()).ToList(); }
                }


                if (sortorder == "desc")
                {
                    if (sortname == "id") { list = first.GetListBy(q => true).OrderByDescending(s => s.id).Select(s => s.ToPOCO()).ToList(); }
                    if (sortname == "name") { list = first.GetListBy(q => true).OrderByDescending(s => s.name).Select(s => s.ToPOCO()).ToList(); }
                    if (sortname == "isNav") { list = first.GetListBy(q => true).OrderByDescending(s => s.isNav).Select(s => s.ToPOCO()).ToList(); }
                 //   if (sortname == "parentName") { list = first.GetListBy(q => true).OrderByDescending(s => s.secondId).Select(s => s.ToPOCO2()).ToList(); }
                }

            }
            else
            {
                list = first.GetListBy(q => true).Select(s => s.ToPOCO()).ToList();

            }
            var gridData = new
            {
                Rows = list,
                Total = 1
            };
            return Json(gridData);
        }
        [HttpPost]
        public void Setdata(FormCollection fc)
        {
            // IBLL.ICate_FisrtBLL first = OperateContext.Current.BLLSession.ICate_FisrtBLL;
            string names = fc["name"];
            int ids = Convert.ToInt32(fc["id"]);
            int navIds = 0;
            string navIdstr = fc["isNav"];
            if (navIdstr == "") { navIds = 0; } else { navIds = Convert.ToInt32(navIdstr); }
            if (ids == 0)
            {
                if (OperateContext.Current.BLLSession.ICate_FisrtBLL.GetListBy(s => s.name == names).ToList().Count() == 0)
                {
                    if (navIds != 0)
                    {
                        MODEL.Cate_Fisrt s = new MODEL.Cate_Fisrt() { name = names, isNav = navIds,type = 1 };
                        OperateContext.Current.BLLSession.ICate_FisrtBLL.Add(s);
                    }
                    else
                    {

                        MODEL.Cate_Fisrt s = new MODEL.Cate_Fisrt() { name = names,isNav = 0,type = 1 };
                        OperateContext.Current.BLLSession.ICate_FisrtBLL.Add(s);
                    }
                }
                

            }
            else
            {
                if (navIds != 0)
                {
                    MODEL.Cate_Fisrt s = new MODEL.Cate_Fisrt() { id = ids, name = names, isNav = navIds };
                    OperateContext.Current.BLLSession.ICate_FisrtBLL.Modify(s, "name", "isNav");
                }
                else
                {
                    MODEL.Cate_Fisrt s = new MODEL.Cate_Fisrt() { id = ids, name = names };
                    OperateContext.Current.BLLSession.ICate_FisrtBLL.Modify(s, "name");
                }

            }
        }
        [HttpPost]
        public void Deldata(FormCollection fc)
        {
            int ids = Convert.ToInt32(fc["id"]);
            //OperateContext.Current.BLLSession.ICate_ThirdBLL.DelBy(s => s.firstId == ids);
            //OperateContext.Current.BLLSession.ICate_SecondBLL.DelBy(s => s.firstId == ids);
            OperateContext.Current.BLLSession.ICate_FisrtBLL.DelBy(s => s.id == ids);
        }
    }
}
